package com.zzx.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.zzx.boot.common.CustomException;
import com.zzx.boot.common.R;
import com.zzx.boot.dto.DishDto;
import com.zzx.boot.mapper.DishMapper;
import com.zzx.boot.pojo.Category;
import com.zzx.boot.pojo.Dish;
import com.zzx.boot.pojo.DishFlavor;
import com.zzx.boot.service.CategoryService;
import com.zzx.boot.service.DishFlavorService;
import com.zzx.boot.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
   @Resource
   private DishMapper mapper;
   @Resource
   private DishFlavorService dishFlavorService;
   @Resource
   private CategoryService categoryService;

    @Override
    @Transactional
    public Page<DishDto> selectPage(Integer page, Integer pageSize, String name) {
        //创建一个分页对象
        Page<Dish> page1 = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getSort);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //查询到记录了
        Page<Dish> dishPage = mapper.selectPage(page1, queryWrapper);
        List<Dish> records = dishPage.getRecords();
        //将分页信息拷贝到这里
        // 将 第一个参数的数据拷贝到第二个参数中 不需要拷贝的属性
        BeanUtils.copyProperties (page1,dishDtoPage,"records");
        List<DishDto> list1 = new ArrayList<>();
        for (Dish record : records) {
            //创建一个对象
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record,dishDto);
            Long id = record.getCategoryId();
            //查询到分类信息
            Category byId = categoryService.getById(id);
           if (byId!=null){
               dishDto.setCategoryName(byId.getName());

           }
            list1.add(dishDto);

        }
        dishDtoPage.setRecords(list1);
        return dishDtoPage;
    }

    @Override
    @Transactional
    public R<String> saveWithFlavor(DishDto dishDto) {
        //插入一条数据
        int insert = mapper.insert(dishDto);
//      雪花算法会先给对象赋值,然后才进行添加操作 所以 赋值的那个对象其实是有值的
        if (insert!=1){
            throw new CustomException("菜品添加失败!");
        }
        //获取之前插入数据的id
        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        boolean b = dishFlavorService.saveBatch(flavors);
        if (!b){
            throw new CustomException("口味表添加失败!");
        }
        return R.success("添加成功");
    }

    @Override
    @Transactional
    public R<String> update(DishDto dishDto) {
        //通过id去修改 菜品
        int i = mapper.updateById(dishDto);
        if (i!=1){
            throw new CustomException("菜品修改失败!");
        }
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        boolean remove = dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }

        boolean b = dishFlavorService.saveBatch(flavors);
        if (!b){
           throw new CustomException("口味修改失败");
        }
        return R.success("修改成功");
    }


}
